#include "factory.h"
//执行对应的命令

//执行cd进入对应目录
int server_cd(pcnode p,train_state_t* ct)
{
	if(!strcmp(ct->file_name,"/"))//如果是cd /命令
	{
		int ret;
		//改code
		p->client.code=0;
		char buf[1000];
        sprintf(buf,"%s/",p->client.c_name);
		//改路径
		strcpy(p->client.path,buf);
		bzero(buf,sizeof(buf));
		ct->data_len=strlen(p->client.path);//将cd后的路径长度装在火车头
		strcpy(ct->buf,p->client.path);//将cd后的路径装在火车车厢
		ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
		SELFFUNC_ERR_CHECK(ret,"send_n");
		ret=send_n(p->client.new_fd,&ct->buf,4+ct->data_len);
 		SELFFUNC_ERR_CHECK(ret,"send_n");
		//写日志
		char* cmd="cd /";
        usercmd_note(p->client.c_name,cmd,p->client.path);
        printf("command cd / success.\n");
	}
	else if(!strcmp(ct->file_name,".."))//如果是cd ..命令
	{
		int ret;
		//改code
		int now_code=p->client.code;
		if(0==now_code)//已经在根目录
		{
			sprintf(ct->buf,"you are already in the root directory!\n");
			ct->data_len=strlen(ct->buf);
			ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
			SELFFUNC_ERR_CHECK(ret,"send_n");
			ret=send_n(p->client.new_fd,&ct->buf,4+ct->data_len);
			SELFFUNC_ERR_CHECK(ret,"send_n");
		}
		else{
			//拿precode
            int now_precode=find_precode(now_code);
			//改code
			p->client.code=now_precode;
			//改路径
			goto_prepath(p->client.path);
			strcpy(ct->buf,p->client.path);//将cd后的路径长度装在火车头
			ct->data_len=strlen(ct->buf);//将cd后的路径装在火车车厢
			ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
			SELFFUNC_ERR_CHECK(ret,"send_n");
			ret=send_n(p->client.new_fd,&ct->buf,4+ct->data_len);
			SELFFUNC_ERR_CHECK(ret,"send_n");
		}
		//写日志
		char* cmd="cd ..";
        usercmd_note(p->client.c_name,cmd,p->client.path);
        printf("command cd .. success.\n");
	}
	else{//如果是cd <dirname>命令
		int belongID,ret;
		//因为不同用户可以有相同文件夹名，故需找到改用户的ID
		find_ID(p->client.c_name,&belongID);
		//找出文件夹名对应code
		int file_code=find_file_code(ct->file_name,belongID,p->client.code);
		if(-1==file_code)//不存在
		{
			sprintf(ct->buf,"The directory name you entered does not exit!\n");
			ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
			SELFFUNC_ERR_CHECK(ret,"send_n");
			ret=send_n(p->client.new_fd,&ct->buf,ct->data_len);
			SELFFUNC_ERR_CHECK(ret,"send_n");
		}
		else{
			//改code
			p->client.code=file_code;
			//改路径
			char buf[1000]={0};
			sprintf(buf,"%s/",ct->file_name);
			strcat(p->client.path,buf);//保存cd 后的路径
			strcpy(ct->buf,p->client.path);
			ct->data_len=strlen(ct->buf);
			ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
			SELFFUNC_ERR_CHECK(ret,"send_n");
			ret=send_n(p->client.new_fd,&ct->buf,ct->data_len);
			SELFFUNC_ERR_CHECK(ret,"send_n");
			//写cmd日志
            char cmd[200];
            sprintf(cmd,"cd %s",ct->file_name);
            usercmd_note(p->client.c_name,cmd,p->client.path);
            printf("command cd %s success.\n",ct->file_name);
		}
	}
}

//执行ls得到该目录下的文件信息发送给客户端
int server_ls(pcnode p,train_state_t* ct)
{
	int ret;
    int belongID;
	find_ID(p->client.c_name,&belongID);
	ls_func(p->client.new_fd,p->client.code,belongID);
	char* cmd="ls";
	usercmd_note(p->client.c_name,cmd,p->client.path);
	printf("command ls success.\n");
}

//执行rm，满足客户端的请求
int server_remove(pcnode p,train_state_t* ct)
{
	int belongID,ret;
    find_ID(p->client.c_name,&belongID);
	char file_md5[200];
    //找目标文件夹,找不到返回-1，找到返回0,并取得code,拿取md5
    ret=find_target_file(p->client.code,ct->file_name,belongID,file_md5);
	if(0==ret)//该文件夹存在
	{
		int link_num;
		//删除文件
		delete_file(p->client.code,ct->file_name,belongID);
		linknum_reduce(file_md5);
		//如果删除后连接数为0则删除表项
		delete_filepool(file_md5);
		sprintf(ct->buf,"remove success!\n");
		ct->data_len=strlen(ct->buf);
		ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
		SELFFUNC_ERR_CHECK(ret,"send_n");
		ret=send_n(p->client.new_fd,&ct->buf,4+ct->data_len);
		SELFFUNC_ERR_CHECK(ret,"send_n");
		//写日志
		char cmd[200]={0};
		sprintf(cmd,"remove %s",ct->file_name);
		usercmd_note(p->client.c_name,cmd,p->client.path);
		printf("command remove %s success.\n",ct->file_name);
	}
}

//通过getcwd拿到当前路径，并发送给客户端
int server_pwd(pcnode p,train_state_t* ct)
{
	int ret;
	strcpy(ct->buf,p->client.path);
	ct->data_len=strlen(ct->buf);
	ret=send_n(p->client.new_fd,&ct->data_len,sizeof(int));
	SELFFUNC_ERR_CHECK(ret,"send_n");
	ret=send_n(p->client.new_fd,&ct->buf,4+ct->data_len);
	SELFFUNC_ERR_CHECK(ret,"send_n");
	//写cmd日志
	char cmd[200];
	sprintf(cmd,"pwd");
	usercmd_note(p->client.c_name,cmd,p->client.path);
	printf("command pwd success.\n");
}

//TOKEN验证
int server_token(int new_fd)
{
	train_t t;
	int ret;
	int data_len;
	char token[1000]={0};
	char usr_name[128]={0};
	ret=recv_n(new_fd,&data_len,4);
	SELFFUNC_ERR_CHECK(ret,"revc_n");
	ret=recv_n(new_fd,&token,data_len);
	SELFFUNC_ERR_CHECK(ret,"revc_n");
	//根据token找到用户名，失败发送空火车
	ret=get_salt_to_username(usr_name,token);
	if(-1==ret)//token错误，发送空火车
	{
		t.data_len=0;
		ret=send_n(new_fd,&t.data_len,sizeof(int));
		printf("token error!\n");
		return -1;
	}
	else{
		printf("token correct!\n");
		return 0;
	}
}
